home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / ShellPanel / NIOpen.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  70 lines

  1. /* File: NIOpen.m - (Interactive) Unix shell version of NIOpenPanel
  2.  *
  3.  * By: Christopher Lane (lane@sumex-aim.stanford.edu)
  4.  *
  5.  * Date: 10 November 1992
  6.  *
  7.  * Copyright: 1991 & 1992 by The Leland Stanford Junior University.
  8.  * This program may be distributed without restriction for non-commercial use.
  9.  */
  10.  
  11. #import <stdlib.h>
  12. #import <getopt.h>
  13. #import <strings.h>
  14. #import <sys/param.h>
  15. #import <nikit/NIOpenPanel.h>
  16. #import <appkit/Application.h>
  17.  
  18. #define USAGE "usage: %s [-d directory] [-t title] [-p prompt]\n"
  19. #define EXIT_USAGE (2)
  20. #define SEPARATOR "/"
  21.  
  22. const char *fullPath(const char *parent, const char *name)
  23. {
  24.     static char buffer[MAXPATHLEN];
  25.     
  26.     if(parent == NULL) return name;
  27.     
  28.     if(strcmp(strcpy(buffer, parent), SEPARATOR) != 0) (void) strcat(buffer, SEPARATOR);
  29.     
  30.     return strcat(buffer, name);
  31. }
  32.  
  33. void main(int argc, char *argv[])
  34. {
  35.     const char *title = NULL, *prompt = NULL, *path = NULL;
  36.     int option, status = EXIT_SUCCESS;
  37.     
  38.     NXApp = [Application new];
  39.  
  40.     while ((option = getopt(argc, argv, "d:t:p:")) != EOF)
  41.         switch (option) {
  42.         case 'd': path = optarg; break;
  43.         case 't': title = optarg; break;
  44.         case 'p': prompt = optarg; break;
  45.         default : status = EXIT_USAGE;
  46.         }
  47.     if (optind < argc) status = EXIT_USAGE;
  48.     
  49.     if (status == EXIT_USAGE) (void) fprintf(stderr, USAGE, [NXApp appName]);
  50.     else {
  51.         int flags, context = [NXApp activateSelf:YES];    
  52.         NIOpenPanel *panel = [NIOpenPanel new];
  53.     
  54.         if (title != NULL) [panel setPanelTitle:(char *) title];
  55.         if (prompt != NULL) [panel setListTitle:(char *) prompt];
  56.         if (path != NULL) [panel setDirectoryPath:path];
  57.     
  58.         if ((flags = [panel runModal]) == NX_OKTAG) (void) puts(fullPath([panel domain], [panel directory]));
  59.         else status = flags;
  60.                 
  61.         [panel free];
  62.         
  63.         if (context) (void) [NXApp activate:context];
  64.         }
  65.         
  66.     [NXApp free];
  67.     
  68.     exit(status);
  69. }
  70.